/**
 * Created by weiyio on 2017/8/26.
 */
// 存储上传的图片

var upload_imgs = {} ;
var id = 1;
// 消息提示框
var name_zh_msg = $("#name_zh_msg");
var license_img_msg = $("#license_img_msg");
var logo_msg = $("#logo_msg");
name_zh_msg.hide();
license_img_msg.hide();
logo_msg.hide();

$("#name_zh").focus(function(){
       name_zh_msg.hide();
});

Dropzone.autoDiscover = false;
$("#dropz_logo").dropzone({
    url: "{% url 'smErp:file_upload' %}",
    addRemoveLinks: true,
    dictRemoveLinks: "x",
    dictCancelUpload: "x",
    dictRemoveFile:"删除",
    maxFiles: 1,
    maxFilesize: 5,
    acceptedFiles: "image/*",
    init: function() {
        this.on("success", function(file, data){
            logo_msg.hide();
            console.log("File " + file.name + "uploaded");
            var logo_obj = $("<input id='logo' name='logo' style='display:none;' value='" + data.img_url + "' readonly/>");
            $("#dropz_logo").append(logo_obj);
            file.id = id;        // 换成服务端唯一的索引，可以后面删除对应的input
            upload_imgs[file.id] = logo_obj;
            id += 1;
        });
        this.on("removedfile", function(file) {
            console.log("File " + file.name + "removed");
            upload_imgs[file.id].remove();
        });
    }
});

$("#dropz_licence").dropzone({
    url: "{% url 'smErp:file_upload' %}",
    addRemoveLinks: true,
    dictRemoveLinks: "x",
    dictCancelUpload: "x",
    dictRemoveFile:"删除",
    maxFiles: 5,
    maxFilesize: 5,
    acceptedFiles: "image/*",
    init: function() {
        this.on("success", function(file, data) {
            license_img_msg.hide();
            console.log("File " + file.name + "uploaded");
                var logo_obj = $("<input id='license_img' name='license_img' style='display:none;' value='" + data.img_url + "' readonly/>");
                $("#dropz_licence").append(logo_obj);
                file.id = id;        // 换成服务端唯一的索引，可以后面删除对应的input
                upload_imgs[file.id] = logo_obj;
                id += 1;
        });
        this.on("removedfile", function(file) {
            console.log("File " + file.name + "removed");
            upload_imgs[file.id].remove();
        });
    }
});

$("#brandBtn").on("click", function(){
    var name_zh = $("#name_zh").val();
    var license_img = $("#license_img").val();
    var logo = $("#logo").val();
    if(!name_zh || !license_img || !logo){
        if(!name_zh){
            name_zh_msg.css("color","red");
            name_zh_msg.html("请输入品牌名称");
            name_zh_msg.show();
        }
        if(!license_img){
            license_img_msg.css("color","red");
            license_img_msg.html("请上传品牌证书");
            license_img_msg.show();
        }
        if(!logo){
            logo_msg.css("color","red");
            logo_msg.html("请上传品牌logo");
            logo_msg.show();
        }
        return ;
    }
    var brand_info = $("#form").serialize();
    $.post("{% url 'smErp:create_brand' %}", brand_info,
            function(data, status, xhr) {
                if(status == "success") {
                    if(data.result == 0) {
                        // 提交品牌信息
                        //alert(brand_info);
                        // 切换状态
                        $("#set2").addClass("current");
                        $("#set1").removeClass("current");
                        $("#set1").addClass("complete");
                        $("#set1").children("div[class='step_inner']").children("i[class='icon_step']").html("");
                        $("#set1").children(".step_line").css("background-color", "#44b549");
                        // 改变进度 栏状态
                        $("#step1").hide();
                        $("#step2").show();
                    }else{
                        layer.msg(data.reason);
                    }
                }else{
                    layer.msg(status);
                }

            },
    "json");
});
